#ifndef MESSAGE_H
#define MESSAGE_H

#include <QWidget>
class QLabel;

#ifndef FOR_ALL_MSG_TYPE
#define FOR_ALL_MSG_TYPE(F)\
    F(Info,"提示",QColor(0,190,90),QColor(30,100,80),QColor(240,240,240))\
    F(Warning,"警告",QColor(225,170,0),QColor(160,100,30),QColor(240,240,240))\
    F(Critical,"错误",QColor(220,0,0),QColor(160,10,10),QColor(240,240,240))\
    F(Unknow,"未知",QColor(220,0,0),QColor(160,10,10),QColor(240,240,240))
#endif

/*弹窗种类的枚举量*/
enum class MsgType
{
    /*C1按钮背景颜色，C2底色，C3是文本颜色*/
#define CREATE_ENUM(E,S,C1,C2,C3) E,
    FOR_ALL_MSG_TYPE(CREATE_ENUM)
#undef CREATE_ENUM
};

/*不需要QPushButton那么臃肿，所以自定义即可*/
class CloseButton : public QWidget
{
    Q_OBJECT
public:
    using QWidget::QWidget; /*继承构造*/
signals:
    void clicked();
public:
    /*设置获取消息类型*/
    void setMsgType(MsgType msgType);
    MsgType getMsgType() const;
    /*设置、获取窗体透明度*/
    void setOpacity(qreal opacity);
    qreal getOpacity() const;
protected:
    void mousePressEvent(QMouseEvent *event) override;
    void paintEvent(QPaintEvent* ) override;
public:
    static QColor backgroundColor(MsgType msgType);
private:
    MsgType m_msgType;
    qreal   m_opacity;
};

/*自定义Label*/
class StyleLabel : public QWidget
{
public:
    using QWidget::QWidget;
public:
    /*设置获取消息类型*/
    void setMsgType(MsgType msgType);
    MsgType getMsgType() const;
    /*设置、获取窗体透明度*/
    void setOpacity(qreal opacity);
    qreal getOpacity() const;
    /*设置、获取文本*/
    void setText(const QString& text);
    QString getText() const;
protected:
    void paintEvent(QPaintEvent* ) override;
private:
    MsgType m_msgType;
    qreal   m_opacity;
    QString m_text;
};

/*消息弹窗*/
class Message : public QWidget
{
    Q_OBJECT
    /*注册属性方便使用动画*/
    Q_PROPERTY(qreal opacity READ getOpacity WRITE setOpacity)
private:
    explicit Message(QWidget *parent = nullptr);
    ~Message();

    Message(const Message&)=delete;
    Message& operator=(const Message&)=delete;
    Message(Message&&)=delete;
    Message& operator=(Message&&)=delete;
public:
    /*弹窗提示,只公开该函数*/
    static void showInfomation(const QString& msg,QWidget* w);
    static void showWarning(const QString& msg,QWidget* w);
    static void showCritical(const QString& msg,QWidget* w);
    static void showMessage(const QString& msg,MsgType type,QWidget* w);
protected:
    /*绘制*/
    void paintEvent(QPaintEvent *) override;
    /*事件过滤器*/
    bool eventFilter(QObject *obj, QEvent *event) override;
public:
    /*获取枚举对应的字符*/
    static QString toString(MsgType type);
    /*获取字体颜色*/
    static QColor fontColor(MsgType type);
    /*获取背景颜色*/
    static QColor backgroundColor(MsgType type);
private:
    /*设置、获取窗体透明度*/
    void setOpacity(qreal opacity);
    qreal getOpacity() const;
    /*设置类型*/
    void setMsgType(MsgType msgType);
    MsgType getMsgType() const;
    /*设置文本*/
    void setMessage(const QString& msg);
    QString getMessage() const;
private:
    /*窗体透明度*/
    qreal m_opacity;
    /*文本*/
    QLabel* m_label;
    /*关闭按钮*/
    CloseButton* m_button;
    /*消息类型*/
    MsgType m_msgType=MsgType::Unknow;
    /**/
    StyleLabel* m_typeLabel;
};

#endif // MESSAGE_H
